package com.meta.nd.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.meta.common.ConstDef;
import com.meta.nd.factory.NvFactory;
import com.meta.nd.vo.Fd;
import com.meta.nd.vo.Fv;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import com.meta.util.DateTool;

public class NodeUtil {
		
	private static final SimpleDateFormat sdf= new SimpleDateFormat(ConstDef.DATE_FORMAT_DEFAULT);
	
	private static final SimpleDateFormat sdtf = new SimpleDateFormat(ConstDef.DATETIME_FORMAT_DEFAULT);
	
public static Nv nd2Nv( Nd nd ) {
	Nv nv = singleNd2Nv( nd );
	nv.setOperationFlag(ConstDef.OPERATION_FLAG_NEW);
	for ( Nd childNd : nd.getChildren() ) {
		nv.setChildNv(childNd.getNdName(), new ArrayList<Nv>());
		for ( int i =0; i < childNd.getMinCount(); i++ ) {
			Nv childNv = nd2Nv( childNd );
			nv.addChildNv(childNd.getNdName(), childNv);
		}
	}
	if ( nd.isCursive() ){
		nv.setChildNv(nd.getNdName(), new ArrayList<Nv>());
	}
	return nv;
}
	
	
	private static Nv singleNd2Nv ( Nd nd ) {
		Nv nv =  NvFactory.createNv(nd);
		nv.setMetaNdCode(nd.getNdCode());
		nv.setActualNdCode(nd.getNdCode());
		for ( Fd fd : nd.getFds() ) {
			Fv fv = fd2Fv(fd);
			nv.getFvs().put(fd.getFdName(), fv);
		}
		return nv;
	}
	
public static Fv fd2Fv(Fd fd ) {
	Fv fv = new Fv ();
	fv.setCode(fd.getFdCode());
	fv.setName(fd.getFdName());
	fv.setDataType(fd.getDataType());
	return fv;
}
	
	public static boolean matchDataType ( String dataType, Object value) {
		
		if ( value == null )
			return true;
		
		
		if (dataType.equals(ConstDef.DATA_TYPE_STRING)) {
			if ( value instanceof String )
				return true;
		}
		else if ( dataType.equals(ConstDef.DATA_TYPE_DATE ) || dataType.equals(ConstDef.DATA_TYPE_DATETIME )) {
			if ( value instanceof Date )
				return true;
						
		}else if ( dataType.equals( ConstDef.DATA_TYPE_INT )) {
			
			if ( value instanceof Integer )
				return true;
			
		}else if (dataType.equals(ConstDef.DATA_TYPE_LONG )) {
			if ( value instanceof Integer ||  value instanceof Long)
				return true;
			
		}else if (dataType.equals(ConstDef.DATA_TYPE_BOOLEAN)) {
			
			if ( value instanceof Boolean )
				return true;
			
		}
		else if (dataType.equals(ConstDef.DATA_TYPE_FLOAT)) {
			
			if ( value instanceof Float || value instanceof Double )
				return true;	
		}
		else if ( dataType.equals(ConstDef.DATA_TYPE_STRING_LIST)){
			if ( value == null || value instanceof ArrayList){
				return true;
			}
		}
		return false;
		
	}	
	
	public static SimpleDateFormat getSimpleDateFormat() {
		return sdf;	
	}

	public static SimpleDateFormat getSimpleDatetimeFormat() {
		return sdtf;	
	}
	
	
	public static Date parseDate(String value ) {
		
		if ( value == null || value.equals("") || value.equals("null"))
			return null;
		
		try {			
			return sdf.parse(value);
		}catch (Exception e ) {
			throw new RuntimeException("无效的日期格式：" + value,e);
		}
	}
	
	public static Date parseDatetime(String value ) {
		
		if ( value == null || value.equals("") || value.equals("null"))
			return null;
		
		try {			
			return sdtf.parse(value);
		}catch (Exception e ) {
			throw new RuntimeException("无效的日期格式：" + value,e);
		}
	}
	
	
	
	public static String formatDate(Date date ) {
		if ( date == null )
			return null;
		else
			return sdf.format(date);
	}
	
	
	public static String formatDatetime(Date date ) {
		if ( date == null )
			return null;
		else
			return sdtf.format(date);
	}
	
	public static Date getSystemTime() {		
		return DateTool.getSystemDate();
	}
	
}
